﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
public partial class index : System.Web.UI.Page
{
//    public DataList[] datalist_array;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PreRenderComplete += new EventHandler(InitPage);
        }
    }

    protected void InitPage(object sender, EventArgs e)
    {
        InitData();
    }

    protected void InitData()
    {
        List<District> districts=null;
        Int16 districtId=0;
        Int16 servedDistrictId=0;

        if (null != Session["RestaurantDistrictSelected"])
            districtId = Convert.ToInt16(Session["RestaurantDistrictSelected"].ToString());
        if (null != Session["ServedDistrictSelected"])
            servedDistrictId = Convert.ToInt16(Session["ServedDistrictSelected"].ToString());

        if (districtId>0)
        {
            districts = BLL.Command.GetDistricts("district_id="+districtId);
            districts[0].TopNum= 0;
        }
        else
            districts = BLL.Command.GetDistricts(BLL.Command.DistrictType.ForRestaurant);

        if (servedDistrictId > 0)
        {
            foreach (District district in districts)
            {
                List<Restaurant> validRestaurants = new List<Restaurant>();
                List<Restaurant> restaurants = district.RestaurantsOfDistrict;
                foreach (Restaurant restaurant in restaurants)
                {
                    if (restaurant.DoesServe(servedDistrictId))
                        validRestaurants.Add(restaurant);
                }
                district.RestaurantsOfDistrict = validRestaurants;
            }
        }

        district_datalist.DataSource = districts;
        district_datalist.DataBind();
    }

    protected void FilterSelectedIndexChanged(object sender, Int16 districtId, Int16 servedDistrictId)
    {
        InitData();
    }
}
